<?php
/**
 * Created by PhpStorm.
 * User: zjr
 * Date: 2021/5/11
 * Time: 13:28
 */

namespace App\Utils;

use Illuminate\Support\Facades\Redis;

class RedisUtils
{

    //设置用户信息
    public static function setUserInfo($token,$userInfo){
        Redis::setex($token,3600*24*7,json_encode($userInfo));
    }
    public static function setStoreUserInfo($token,$userInfo){
        Redis::setex($token,3600*24*7,json_encode($userInfo));
    }
    //获取用户信息
    public static function getUserInfo($token){
        return Redis::get($token);
    }
    public static function getStoreUserInfo($token){
        return Redis::get($token);
    }

    //设置accesstoken信息
    public static function setAccessToken($accesstoken){
        Redis::setex('alipay_token',3600,$accesstoken);
    }

    public static function getAccessToken(){
        return Redis::get('alipay_token');
    }

    /**
     * 蚂蚁联盟链Token
     * @param $accesstoken
     */
    public static function setNftToken($accesstoken)
    {
        Redis::setex('nft_token',1700,$accesstoken);
    }

    public static function getNftToken()
    {
        return Redis::get('nft_token');
    }


    /**
     * 缓存手机验证码
     * @param $mobile
     * @param $code
     */
    public static function setMobileCode($mobile,$code)
    {
        $key = 'cache-'.$mobile;
       return Redis::setex($key,300,$code);
    }

    public static function getMobileCode($mobile)
    {
        $key = 'cache-'.$mobile;
        return Redis::get($key);
    }

    /**
     * 设置手机号验证码当天发送次数
     * @param $mobile
     * @return mixed
     */
    public static function setMobileCodeNumber($mobile)
    {
        $key = date("Y-m-d").'_'.$mobile;
        $key_num = Redis::get($key);
        if(empty($key_num)){
            return Redis::setex($key,24*3600,1);
        }else{
            return Redis::setex($key,24*3600,$key_num+1);
        }
    }

    public static function getMobileCodeNumber($mobile)
    {
        $key = date("Y-m-d").'_'.$mobile;
        return Redis::get($key);
    }

    /**
     * 设置mail邮件验证码
     * @param $emil
     * @param $code
     * @return mixed
     */
    public static function setEmailCode($mail,$code)
    {
        $key = 'cache-'.$mail;
        return Redis::setex($key,60*5,$code);
    }

    public static function getEmailCode($mail)
    {
        $key = 'cache-'.$mail;
        return Redis::get($key);
    }


    /**
     * 获取配置
     * @return array|mixed
     */
    public static function getSysConfig()
    {
        $key = 'sys_config';
        $data = Redis::get($key);
        if(empty($data)){
            $data = Config::query() -> first();
            if(!empty($data)){
                $info = $data -> toArray();
                Redis::setex($key,3600*4,json_encode($info));
            }
        }else{
            $info = json_decode($data,true);
        }
        return $info;
    }

    public static function Del($key)
    {
        return Redis::del($key);
    }
}
